package cn.jolyoulu.adminweb.controller.sys;


import cn.jolyoulu.common.web.security.permission.Permission;
import cn.jolyoulu.adminweb.entity.bo.query.SysRoleQueryBO;
import cn.jolyoulu.adminweb.entity.vo.SysRoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.jolyoulu.adminweb.entity.bo.form.SysRoleFormBO;
import cn.jolyoulu.adminweb.service.ApiSysRoleService;
import cn.jolyoulu.common.mybatis.plus.plugin.page.JlPage;
import cn.jolyoulu.common.web.entity.ResultInfo;

import java.util.List;

/**
 * 系统管理/角色管理
 */
@RestController
@RequestMapping("/api/sys/role")
public class ApiSysRoleController {

    @Autowired
    private ApiSysRoleService apiSysRoleService;

    /**
     * 列表查询
     */
    @Permission({"sys:role:basic"})
    @GetMapping("/pageList")
    public ResultInfo<JlPage<SysRoleVO>> pageList(SysRoleQueryBO bo){
        JlPage<SysRoleVO> res = apiSysRoleService.pageList(bo);
        return ResultInfo.success(res);
    }

    /**
     * 角色信息
     */
    @Permission({"sys:role:basic"})
    @GetMapping("/get/{id}")
    public ResultInfo<SysRoleVO> getById(@PathVariable String id) {
        SysRoleVO res = apiSysRoleService.getById(id);
        return ResultInfo.success(res);
    }

    /**
     * 添加角色
     */
    @Permission({"sys:role:add"})
    @PostMapping("/add")
    public ResultInfo add(@RequestBody @Validated SysRoleFormBO sysRoleFormBO){
        apiSysRoleService.add(sysRoleFormBO);
        return ResultInfo.success();
    }

    /**
     * 编辑角色
     */
    @Permission({"sys:role:edit"})
    @PutMapping("/edit/{id}")
    public ResultInfo edit(@PathVariable String id,@RequestBody SysRoleFormBO sysRoleFormBO){
        sysRoleFormBO.setId(id);
        apiSysRoleService.update(sysRoleFormBO);
        return ResultInfo.success();
    }

    /**
     * 删除角色
     */
    @Permission({"sys:role:del"})
    @DeleteMapping("/del")
    public ResultInfo del(@RequestBody List<String> ids){
        apiSysRoleService.delByIds(ids);
        return ResultInfo.success();
    }

    /**
     * 角色拥有菜单信息
     */
    @Permission({"sys:role:basic"})
    @GetMapping("/get/menu/{id}")
    public ResultInfo getMenu(@PathVariable String id){
        List<String> res = apiSysRoleService.getMenuIds(id);
        return ResultInfo.success(res);
    }

    /**
     * 分配权限菜单
     */
    @Permission({"sys:role:edit"})
    @PostMapping("/allot/menu/{id}")
    public ResultInfo allotMenu(@PathVariable String id,@RequestBody List<String> menuIds){
        apiSysRoleService.allotMenu(id,menuIds);
        return ResultInfo.success();
    }
}
